package data

import (
	"github.com/astaxie/beego/orm"
	_ "github.com/go-sql-driver/mysql"
)

type CarrierOwnerDto struct {
	Id            int    `orm:"pk;column(id)"`
	Stars         int    `orm:"column(stars)"`
	VehicleType   int    `orm:"column(vehicle_type)"`
	VehicleLength int    `orm:"column(vehicle_len)"`
	VehicleLoad   int    `orm:"column(vehicle_load)"`
	VehicleNo     string `orm:"column(vehicle_no)"`
}

type CarrierOwnerGetDto struct {
	Name               string `orm:"column(name)"`
	Stars              int    `orm:"column(stars)"`
	Phone              string `orm:"column(phone)"`
	VehicleType        int    `orm:"column(vehicle_type)"`
	VehcileLen         int    `orm:"column(vehicle_len)"`
	VehicleLoad        int    `orm:"column(vehicle_load)"`
	VehicleNo          string `orm:"column(vehicle_no)"`
	VehcilePhotoUrl    string `orm:"column(vehicle_photo)"`
	DrivingLicenseUrl  string `orm:"column(driving_license)"`
	VehicleLicenseUrl1 string `orm:"column(vehicle_license_1)"`
	VehicleLicenseUrl2 string `orm:"column(vehicle_license_2)"`
}

func init() {
	orm.RegisterModel(new(CarrierOwnerDto))
}

func (c *CarrierOwnerDto) TableName() string {
	return "m_carrier_owner"
}

func (r MemberRepo) UpdateCarrierOwner(dto *CarrierOwnerDto) error {
	mbr := &MemberDto{Id: dto.Id, TypeId: 2}
	o := orm.NewOrm()
	_, err := o.Update(dto)
	if err == nil {
		if err := r.UpdateAuthState(dto.Id, 2); err == nil {
			if mbrType, err := r.GetMemberType(dto.Id); err == nil {
				if mbrType == "0" {
					_, err = o.Update(mbr, "TypeId")
					return err
				}
			}
		}
	}
	return err
}

func (r MemberRepo) GetCarrierOwner(id int) (dto CarrierOwnerGetDto, err error) {
	o := orm.NewOrm()
	dto = CarrierOwnerGetDto{}
	err = o.Raw(SQL_Mbr_Get_Carrier_Owner, id).QueryRow(&dto)
	return
}
